//
//  UCInfomationView.m
//  ICAlertViewAndActionSheet
//
//  Created by JianWei Chen on 16/7/26.
//  Copyright © 2016年 JianWei Chen. All rights reserved.
//

#import "AlertViewManager.h"
#import <UIKit/UIKit.h>
/*
  全局静态变量，让类方法中的对象持有
*/
static int current = 0;

@interface  AlertViewManager()<UIActionSheetDelegate,UIAlertViewDelegate>

@property (strong, nonatomic) NSMutableArray *clickArray;

@end

@implementation AlertViewManager

+ (AlertViewManager *)shareManager
{
    static AlertViewManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[AlertViewManager alloc]init];
    });
    return manager;
}

- (NSMutableArray *)clickArray
{
    if (!_clickArray) {
        _clickArray = [[NSMutableArray alloc]init];
    }
    return _clickArray;
}

- (void)initWithTitle:(NSString*)title message:(NSString *)messge cancleButtonTitle:(NSString *)cancleButtonTitle OtherButtonsArray:(NSArray*)otherButtons clickAtIndex:(ClickAtIndexBlock) clickAtIndex;

{
    [self.clickArray addObject:clickAtIndex];
    
    UIAlertView  *alertView = [[UIAlertView alloc] initWithTitle:title message:messge delegate:self cancelButtonTitle:cancleButtonTitle otherButtonTitles: nil];
    for (NSString *otherTitle in otherButtons) {
        [alertView addButtonWithTitle:otherTitle];
    }
    
    alertView.tag = ++current;
    
    [alertView show];
}

- (void)initWithMessage:(NSString *)messge cancleButtonTitle:(NSString *)cancleButtonTitle OtherButtonsArray:(NSArray *)otherButtons clickAtIndex:(ClickAtIndexBlock)clickAtIndex
{
    [self initWithTitle:@"提示" message:messge cancleButtonTitle:cancleButtonTitle OtherButtonsArray:otherButtons clickAtIndex:clickAtIndex];
}

- (void)initWithMessage:(NSString *)messge cancleButtonTitle:(NSString *)cancleButtonTitle clickAtIndex:(ClickAtIndexBlock)clickAtIndex
{
    [self initWithTitle:@"提示" message:messge cancleButtonTitle:cancleButtonTitle OtherButtonsArray:nil clickAtIndex:clickAtIndex];
}

- (void)initWithEditActionTitle:(NSString *)title message:(NSString *)messge cancleButtonTitle:(NSString *)cancleButtonTitle OtherButtonsArray:(NSArray *)otherButtons clickAtIndex:(ClickAtIndexBlock)clickAtIndex
{
    [self.clickArray addObject:clickAtIndex];
    
    UIAlertView  *alertView = [[UIAlertView alloc] initWithTitle:title message:messge delegate:self cancelButtonTitle:cancleButtonTitle otherButtonTitles: nil];
    for (NSString *otherTitle in otherButtons) {
        [alertView addButtonWithTitle:otherTitle];
    }
    
    alertView.tag = ++current;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alertView show];
}


#pragma mark   UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    ClickAtIndexBlock block = self.clickArray[alertView.tag - 1];
    if (block){
        block(buttonIndex,alertView);
    }
}
//-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
//{
//    _ClickAtIndexBlock = nil;
//}
@end
